package cn.zou.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zou.blog.dao.ArticleDao;
import cn.zou.blog.entry.Article;
import cn.zou.blog.service.ArticleService;
import cn.zou.core.page.Page;
import cn.zou.core.util.luncene.IndexUtils;
import cn.zou.core.util.luncene.LunceneUtils;

@Service("articleService")
@Transactional
public class ArticleServiceImpl implements ArticleService {
	private static Logger logger=Logger.getLogger(ArticleServiceImpl.class);
	@Autowired
	private ArticleDao articleDao;
	public void addArticle(Article article,HttpServletRequest request) throws Exception {
		// TODO 自动生成的方法存根
		
			articleDao.addArticle(article);
			
			String path=request.getSession().getServletContext().getRealPath("/lucene/article");
			if(IndexUtils.getPath()==null){
				IndexUtils.setPath(path);
			}
			IndexUtils.IndexCreta(LunceneUtils.BeanConversionDocument(article));
			
		
	}

	public void updateArticleById(Article article,HttpServletRequest request) throws Exception {
		articleDao.updateArticleById(article);
		String path=request.getSession().getServletContext().getRealPath("/lucene/article");
		if(IndexUtils.getPath()==null){
			IndexUtils.setPath(path);
		}
		IndexUtils.upadteIndex(LunceneUtils.BeanConversionDocument(article));
		
	}
	@Transactional(readOnly=true)
	public Article findByIdNotGuestBook(String id) {
		// TODO 自动生成的方法存根
		return null;
	}
	@Transactional(readOnly=true)
	public Article findByIdAndGuestBook(String id) {
		// TODO 自动生成的方法存根
		return null;
	}

	public void deleteArticleByid(String id,HttpServletRequest request) {
		// TODO 自动生成的方法存根
		articleDao.deleteArticleByid(id);
		String path=request.getSession().getServletContext().getRealPath("/lucene/article");
		if(IndexUtils.getPath()==null){
			IndexUtils.setPath(path);
		}
		IndexUtils.deletIndex(id);
	}

	public ArrayList<Article> findArticleByTitle(String titleOrcontant) {
		
		return null;
	}
	@Transactional(readOnly=true)
	public Page<Article> findByTitle(Page page) {
		// TODO 自动生成的方法存根
		Page<Article> newPage=new Page<Article>(page.getPageSize(),page.getPageNo(),articleDao.findCountByTitle(page));
		newPage.setItmes(articleDao.findArticleByTitle(page));
		
		
		return newPage;
		
	}
	@Transactional(readOnly=true)
	public Article findById(String id) {
		
		return articleDao.findById(id);
	}

	public void upadteStateArticle(String id, String state, HttpServletRequest request) {
		Article article=new Article();
		article.setState(state);
		article.setId(id);
		articleDao.upadteStateArticle(article);
		String path=request.getSession().getServletContext().getRealPath("/lucene/article");
		if(IndexUtils.getPath()==null){
			IndexUtils.setPath(path);
		}
		if(state.equals("0")){
			IndexUtils.deletIndex(id);
		}else{
			article=articleDao.findById(id);
			IndexUtils.IndexCreta(LunceneUtils.BeanConversionDocument(article));
		}
		
	}

	public Page<Article> findArticles(Page<Article> page,HttpServletRequest request,String content,String field) {
		if(IndexUtils.getPath()==null){
			String path=request.getSession().getServletContext().getRealPath("/lucene/article");
			IndexUtils.setPath(path);
		}
		List<Article> indexSerch = IndexUtils.indexSerch(content, new Sort(new SortField("modifyTime",SortField.Type.LONG ,true)), page.getPageNo(), page.getPageSize(),false,field);
		int indexSerchCount = IndexUtils.indexSerchCount(content, field);
		Page<Article> newpage=new Page<Article>(page.getPageSize(),page.getPageNo(), indexSerchCount);
		newpage.setItmes(indexSerch);
		return newpage;
		
	}

	public Article findByIdInLuncene(String id, HttpServletRequest request) {
		if(IndexUtils.getPath()==null){
			String path=request.getSession().getServletContext().getRealPath("/lucene/article");
			IndexUtils.setPath(path);
		}
		List<Article> indexSerch = IndexUtils.indexSerch(id, Sort.INDEXORDER, 0, 1,true,"id");
		return indexSerch.get(0);
	}

	public List<Article> findnewArticle() {
		
		return articleDao.findnewArticle();
	}

	

	

}
